<template>
  <div class="app-container">
    <div v-if="!detailVisible">
      <div class="search">
        <el-input v-model="form.name" style="width: 200px;margin-right: 20px" clearable placeholder="请输入任务名称" />
        <el-select v-model="form.status" clearable filterable placeholder="请选择">
          <el-option
            v-for="item in subStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button type="primary" icon="el-icon-search" style="margin-left: 40px" @click="setResult">查询</el-button>
        <el-button type="primary" icon="el-icon-refresh" @click="reset">重置</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column prop="id" label="工单编号" width="100" />
        <el-table-column prop="name" label="任务名称" width="240">
          <template slot-scope="scope">
            <div>
              {{ scope.row.task && scope.row.task.name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="收单时间">
          <template slot-scope="scope">
            <div>
              {{ getTimeFormate(scope.row.updated_at) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="填报人">
          <template slot-scope="scope">
            <div style="color: #ffad32">{{ scope.row.report_log && scope.row.report_log.operator && scope.row.report_log.operator.name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="填报时间">
          <template slot-scope="scope">
            <div>{{ scope.row.report_log && getTimeFormate(scope.row.report_log.updated_at) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="审核人">
          <template slot-scope="scope">
            <div>{{ scope.row.review1_log && scope.row.review1_log.operator && scope.row.review1_log.operator.name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="审核时间">
          <template slot-scope="scope">
            <div>{{ scope.row.review1_log && getTimeFormate(scope.row.review1_log.updated_at) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <div>
              {{ getStatus(scope.row.status) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleSave(scope.row, 'detail')">详情</el-button>
            <!-- <el-button v-if="+scope.row.status === 1 || +scope.row.status === 3" type="text" @click="handleSave(scope.row)">催单</el-button> -->
            <el-button v-if="(+scope.row.status === 2 || +scope.row.status === 5) && btn2Visible" type="text" @click="handleSave(scope.row,'init')">初审</el-button>
            <el-button v-if="+scope.row.status === 4 && btnVisible" type="text" @click="handleSave(scope.row,'repeat')">复审</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :total="total" :limit="10" :background="false" :page="currentPage" @pagination="handleCurrentChange" @pageSize="handleSizeChange" />
    </div>
    <div v-if="detailVisible">
      <Detail :item="item" :task-list="taskList" :district-list="districtList" :type="type" @goBack="goBack" @updateResult="updateResult" />
    </div>
  </div>
</template>
<script>
import moment from 'moment'
import { getWorkFillList } from '@/api/workOrder'
import Detail from './detail.vue'
import { getCookie } from '@/api/common'
export default {
  components: {
    Detail
  },
  data() {
    return {
      tableData: [],
      taskList: [],
      districtList: [],
      btn2Visible: true,
      form: {
        name: '',
        status: ''
      },
      type: '',
      item: {},
      listLoading: false,
      detailVisible: false,
      total: 0,
      currentPage: 1,
      pageSizes: 10,
      btnVisible: false,
      subStatus: [
        {
          value: '1',
          label: '待处理'
        }, {
          value: '2',
          label: '待初审'
        },
        {
          value: '3',
          label: '初审驳回'
        },
        {
          value: '4',
          label: '待复审'
        },
        {
          value: '5',
          label: '复审驳回'
        },
        {
          value: '6',
          label: '已完结'
        }
      ]
    }
  },
  created() {
    this.fetchData()
    // this.getCookieToken()
    const roles = JSON.parse(localStorage.getItem('roles'))
    roles.map(item => {
      if (item === '市级审核员') {
        this.btnVisible = true
        this.btn2Visible = false
      } else if (item === '市级管理员') {
        this.btn2Visible = false
      }
    })
    // this.getTaskList()
    // this.getDicList()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getWorkFillList({ ...this.form, page: this.currentPage, per_page: this.pageSizes }).then(response => {
        this.tableData = response.data
        this.total = response.pagination.total
        this.currentPage = response.pagination.current_page
        this.listLoading = false
      })
    },
    getCookieToken() {
      getCookie().then(response => {
      })
    },
    // getTaskName(id) {
    //   let name = ''
    //   this.taskList.forEach(item => {
    //     if (item.id == id) {
    //       name = item.name
    //     }
    //   })
    //   return name
    // },
    handleCurrentChange(val) {
      this.currentPage = val.page
      this.fetchData()
    },
    handleSizeChange(val) {
      this.pageSizes = val.limit
      this.fetchData()
    },
    reset() {
      this.currentPage = 1
      this.form = {
        name: '',
        status: ''
      }
      this.fetchData()
    },
    setResult() {
      this.currentPage = 1
      this.fetchData()
    },
    updateResult() {
      this.detailVisible = false
      this.fetchData()
    },
    goBack() {
      this.detailVisible = false
    },
    getStatus(status) {
      let label = ''
      this.subStatus.forEach(item => {
        if (item.value == status) {
          label = item.label
        }
      })
      return label
    },
    getTimeFormate(date) {
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    handleSave(row, type) {
      const routeUrl = this.$router.resolve({
        path: '/workReview/reviewdetail',
        query: {
          id: row.id,
          type: type
        }
      })
      window.open(routeUrl.href, '_blank')
      // this.item = row
      // this.type = type
      // this.detailVisible = true
    }
  }
}
</script>
<style lang="scss" scoped>
.search {
  margin-bottom: 20px;
}
</style>
